package pteam;

import simulator.Actor;
import simulator.Robot;
import simulator.Sensor;

public class AndrewActor implements Actor {

	double left, right, center;
	boolean sensorsRead = false;
	boolean firstrun = false;
	Robot r;

	public void act(Robot s) {
		if (!sensorsRead) {
			return;
		}
		if (!firstrun) {
			r = s;

			// s.turnRight();
			// s.waitAngle(90);
			// s.goForward();
			// s.waitDistance(110);
			// System.out.println("Start wait.");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// System.out.println("end wait.");
			// waitForDistance(s, 20);
			firstrun = true;
		}

		try {
			Thread.sleep(1);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//
		// s.turnRight();
		// s.waitAngle(2);

		s.goForward();

		s.waitDistance(1);
		if (right > left) {
			s.turnRight();
		} else {
			s.turnLeft();
		}
		s.waitAngle(2);

	}

	public void waitForDistance(Robot r, int distance) {
		while (center > distance) {
			System.out.println("checking...");

			r.goForward();
			r.waitDistance(1);
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("COMPLETE!!!!!------------------");
	}

	@Override
	public String getMaze() {
		return "Example";
	}

	@Override
	public void updateSensors(Sensor[] sensors) {
		sensorsRead = true;
		System.out.println(center);
		center = sensors[1].getState()[0];
		left = sensors[4].getState()[0];
		right = sensors[2].getState()[0];
	}

}
